package slidingWindow;

import java.util.Scanner;

public class P1763 {
    public String longestNiceSubstring(String s) {
        if (s == null || s.length() < 2) {
            return "";
        }
        String tmp = "";
        for (int size = 2; size <= s.length(); size++) {
            for (int begin = 0; begin <= s.length() - size; begin++) {
                String sub = s.substring(begin, begin + size);
                if (valid(sub) && tmp.length()<sub.length()) {
                    tmp = sub;
                }
            }
        }
        return tmp;
    }

    private boolean valid(String s) {
        int small[] = new int[26];
        int big[] = new int[26];
        for (char c : s.toCharArray()) {
            if ('a' <= c && c <= 'z') {
                small[c - 'a'] = 1;
            } else {
                big[c - 'A'] = 1;
            }
        }
        for (int i = 0; i < 26; i++) {
            if (small[i] != big[i]) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        String s = new P1763().longestNiceSubstring("cChH");
        System.out.println(s);
        Scanner scanner =null;
        Integer.parseInt(scanner.nextLine());
    }
}
